/**
 * 
 */
package br.com.devschool.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;

import br.com.devschool.entity.Aluno;
import br.com.devschool.util.BaseDAO;
import br.com.devschool.util.GenericDAO;

/**
 * @author ATILLA
 *
 */
public class AlunoDAO extends BaseDAO implements GenericDAO<Aluno> {

	@Override
	public boolean save(Aluno object) throws Exception {
		try {
			openSession();
			beginTransaction();
			getSession().save(object);
			commit();
			return true;
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@Override
	public boolean update(Aluno object) throws Exception {
		try {
			openSession();
			beginTransaction();
			getSession().update(object);
			commit();
			return true;
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@Override
	public boolean merge(Aluno object) throws Exception {
		try {
			openSession();
			beginTransaction();
			getSession().merge(object);
			commit();
			return true;
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@Override
	public boolean delete(Aluno object) throws Exception {
		try {
			openSession();
			beginTransaction();
			getSession().delete(object);
			commit();
			return true;
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@Override
	public Aluno findById(int pkey) throws Exception {
		try{
			openSession();
			beginTransaction();
			Criteria criteria = getSession().createCriteria(Aluno.class)
					.add(Restrictions.eq("id", pkey));
			Aluno objAluno = (Aluno) criteria.uniqueResult();
			commit();
			return objAluno;
		}  catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Aluno> findAll() throws Exception {
		try{
			openSession();
			beginTransaction();
			Criteria criteria = getSession().createCriteria(Aluno.class);
			List<Aluno> resultado = criteria.list();
			commit();
			return resultado;
		}  catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}

	@Override
	public int nextvalById() throws Exception {
		try{
			openSession();
			beginTransaction();
			SQLQuery sql = getSession().createSQLQuery("SELECT NEXTVAL('Aluno_id_seq')");
			Integer nextval = Integer.parseInt(sql.uniqueResult().toString());
			commit();
			return nextval;
		}  catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} catch (Exception e) {
			rollback();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			closeSession();
		}
	}
	
}
